<?php
// Initialisation
require('inc/init.inc.php');
require(PATH.'/class/gestion/rapports.class.php');

class rapportpdf extends Rapport{
	
function Pdf(){
	$titre='rapport '.$this->attribut['rap_id'].' ('.(($this->attribut['rap_type']=='v')?'Visite':'Téléphonique').') du '.$this->attribut['rap_date'];
	$pdf=new MonFPDFHeader(utf8_decode($titre));
	$pdf->AddPage();
	$hauteur = 8;

	// Utilisateur
	$uti = '';
	$req =	"SELECT uti_code, uti_nom, uti_prenom
			FROM utilisateurs
			WHERE uti_id='".$this->attribut['rap_uti_id']."'";
	if ($result = $this->db->Query($req)){
		if ($result->num_rows){
			$row = $result->fetch_assoc();
			$uti = $row['uti_code'].' - '.$row['uti_nom'].', '.$row['uti_prenom'];
		}
	}
	$pdf->SetFont('Arial','B',10);
	$y = $pdf->GetY();
	$pdf->MultiCell(95, $hauteur, utf8_decode($uti), 0);


	// Societe
	$bra = '';
	$req =	"SELECT cli_nom, cli_adresse, cli_boitepostale, cli_codepostal, cli_ville, cli_pays
			FROM clients
			WHERE cli_id='".$this->attribut['rap_cli_id']."'";
	if ($result = $this->db->Query($req)){
		if ($result->num_rows){
			$row = $result->fetch_assoc();
			$cli_nom = $row['cli_nom'];
			$ln_cli = Array();
			$ln_cli[] = $row['cli_nom'];
			$ln_cli[] = $row['cli_adresse'];
			$ln_cli[] = $row['cli_boitepostale'];
			$ln_cli[] = implode(' ',array_filter(array($row['cli_codepostal'],$row['cli_ville'])));
			$ln_cli[] = $row['cli_pays'];
			$ln_cli = array_filter($ln_cli);
			$cli = implode("\n",$ln_cli);
		}
	}
	$pdf->SetXY(105,$y);	
	$pdf->MultiCell(95, $hauteur, utf8_decode($this->attribut['rap_objet']), 0, 'R');

	$pdf->SetFont('','');

	$pdf->MultiCell(95, $hauteur, utf8_decode($cli), 0);

	$hauteur = 5;
	
	// Participants
	$pdf->Separateur($hauteur,'Participants');
	$sql="SELECT cli_nom, CONCAT_WS(', ',con_nom, con_prenom) as con_nom, con_email, con_telephone, con_portable".
		' FROM contacts'.
		' JOIN clients ON clients.cli_id = contacts.con_cli_id'.
		' JOIN participants ON participants.par_con_id = contacts.con_id'.
		" WHERE par_rap_id ='".$this->attribut['rap_id']."'";
	if ($result = $this->db->Query($sql)){
		$th = array('Société', 'Nom', 'E-mail', 'Telephone');
		$data= array();
		$largeurs = array(190*0.25,190*0.25,190*0.35,190*0.15);
		while ($row = $result->fetch_assoc()) {
			if ( $row['con_portable'] )
				$tel = $row['con_portable'];
			elseif ( $row['con_telephone'] )
				$tel = $row['con_telephone'];
			else
				$tel = '';
			$data[] = array($row['cli_nom'],$row['con_nom'], $row['con_email'], $tel);
		}
		$pdf->Tableau($th, $data, $largeurs, $hauteur);
	}
	
	// Texte
	$pdf->SetXrel(0);
	$texte = $this->attribut['rap_texte'];
	$texte = str_replace("’", "'", $texte);
	$texte = str_replace("&rsquo;", "'", $texte);
	
	$texte = str_replace("…", "...", $texte);
	//$pdf->MultiCell(0, $hauteur, utf8_decode($texte),0);
	$pdf->WriteHTML($texte, $hauteur, $hauteur);
	
	$hauteur = 5;
	
	// Actions
	$pdf->Separateur($hauteur,'Actions');
	$sql="SELECT act_texte, act_date as date_order, DATE_FORMAT(act_date, '%d/%m/%Y') as act_date, DATE_FORMAT(act_date_fait, '%d/%m/%Y') as act_date_fait, CONCAT_WS(', ', uti_nom, uti_prenom) as uti_nom
		FROM actions
		JOIN utilisateurs ON utilisateurs.uti_id = actions.act_uti_id
		WHERE actions.act_rap_id='".$this->attribut['rap_id']."'
		ORDER BY date_order";

	if ($result = $this->db->Query($sql)){
		$th = array('Pour', 'Action', 'délai', 'Fait le');
		$data= array();
		$largeurs = array(190*0.15,190*0.55,190*0.15,190*0.15);
		while ($row = $result->fetch_assoc()) {
			$data[] = array($row['uti_nom'],$row['act_texte'],$row['act_date'],$row['act_date_fait']);
		}
		$pdf->Tableau($th, $data, $largeurs, $hauteur);
	}
	
	$pdf->Output($cli_nom.'-'.$this->attribut['rap_objet'].'.pdf','D');				
}
}


$c = new rapportpdf($db);
$id = get_param('id', 268);

$c->Lire($id);
$c->Pdf();


echo 'fin';
?>
